# $LastChangedDate: 2011-02-26 11:35:50 -0500 (Sat, 26 Feb 2011) $
# LEGAL: COPYRIGHT (C) 2010 JIM E. BROOKS WWW.PALOMINO3D.ORG
# cmake commands for modules of a project.
# Notes:
# - See module.txt
# - Scope/context:
#   The top-level project (project.cmake) and modules (module.cmake) have their own scope.
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------

# BUILD_MODULE( moduleName )
# Loads cmake files to build a module.
# How to actually build the module is defined by MODULE_BEGIN()..MODULE_END().
# Implementation notes:
# Despite its name, BUILD_MODULE() runs in "project scope"
# so it belongs in project.cmake instead of module.cmake.
MACRO( BUILD_MODULE )
    SET( MODULE_NAME ${ARGV0} )
    SET( MODULES ${MODULES} ${MODULE_NAME} )  # build from basic to complex
ENDMACRO()

#-------------------------------------------------------------------------------

# MODULE_BEGIN( moduleName BUILD_TYPE_LIB | BUILD_TYPE_EXEC | BUILD_TYPE_SPECIAL )
MACRO( MODULE_BEGIN )
    SET( MODULE_NAME ${ARGV0} )
    SET( TOP_DIR ${CMAKE_SOURCE_DIR} )  # because cmake works in a build/module/ dir
    SET( SRC_DIR ${TOP_DIR}/src )  # top-level src dir
    SET( TOOLS_DIR ${TOP_DIR}/tools )
    SET( MODULE_LIB_NAME ${MODULE_NAME} )  # same
    SET( MODULE_SRC_DIR ${SRC_DIR}/${MODULE_NAME} )  # subdir of SRC_DIR
    # MODULE_BUILD_TYPE is very different from CMAKE_BUILD_TYPE and PROJECT_BUILD_TYPE.
    SET( MODULE_BUILD_TYPE "" )
    FOREACH( ARG ${ARGV} )
        IF( ARG MATCHES "BUILD_TYPE_LIB"
         OR ARG MATCHES "BUILD_TYPE_EXEC"
         OR ARG MATCHES "BUILD_TYPE_SPECIAL" )
            SET( MODULE_BUILD_TYPE ${ARG} )
        ENDIF()
    ENDFOREACH()
    IF( MODULE_BUILD_TYPE MATCHES "BUILD_TYPE_LIB" )
        # A module built as a lib should add itself to the var PROJECT_LIBS (linker arg).
        # PARENT_SCOPE allows this "module scope" code to modify a variable in "project scope".
        # Add in correct order for the linker (most dependent/advanced first).
        SET( PROJECT_LIBS ${MODULE_NAME} ${PROJECT_LIBS} PARENT_SCOPE )
    ELSEIF( NOT MODULE_BUILD_TYPE )  # catch error in MODULE_BUILD_TYPE
        MESSAGE( FATAL_ERROR "MODULE_BEGIN() requires a BUILD_TYPE_* arg." )
    ENDIF()
ENDMACRO()

#-------------------------------------------------------------------------------

# MODULE_END( void )
MACRO( MODULE_END )
    IF( MODULE_BUILD_TYPE MATCHES "BUILD_TYPE_EXEC" )
        INCLUDE( ${TOP_DIR}/CMakeModules/build_exec.cmake )
    ELSEIF( MODULE_BUILD_TYPE MATCHES "BUILD_TYPE_LIB" )
        ADD_LIBRARY( ${MODULE_LIB_NAME} ${MODULE_SRC_CXX} ${MODULE_SRC_C} )
    ENDIF()
ENDMACRO()

#-------------------------------------------------------------------------------

# Add a src file as a dependency.
MACRO( ADD_SRC_FILE )
#MACRO( ADD_SRC_FILE SRC_FILE )  # FYI would require IF( ${SRC_FILE} )
    SET( SRC_FILE ${ARGV0} )     # longer but allows more consistent IF( SRC_FILE )
    IF( SRC_FILE MATCHES "[.]c$" )  # C
        SET( MODULE_SRC_C ${MODULE_SRC_C} ${MODULE_SRC_DIR}/${SRC_FILE} )
    ELSEIF( SRC_FILE MATCHES "[.]cc$"
         OR SRC_FILE MATCHES "[.]cxx$" )  # C++
        SET( MODULE_SRC_CXX ${MODULE_SRC_CXX} ${MODULE_SRC_DIR}/${SRC_FILE} )
    ELSE()
        MESSAGE( FATAL_ERROR "ADD_SRC_FILE(): unknown src file ${SRC_FILE}" )
    ENDIF()
ENDMACRO()
